// components/ProtectedRoute.jsx
import { Navigate, useMatches } from "react-router-dom";
import { useEffect } from "react";
const ProtectedRoute = ({ children }) => {
  const matches = useMatches();
  useEffect(() => {
    const currentRoute = matches[matches.length - 1];
    const pageTitle ='月库-' +  currentRoute?.handle?.title || "我的网站";
    document.title = pageTitle;
  }, [matches]);
  // 检查登录状态（实际项目中从 Context/Redux 获取）
  const isLogin = localStorage.getItem("isLogin");
  const from = window.location.pathname; // 记录来源路径
  // 如果未登录，重定向到登录页；否则则渲染子路由
  return !isLogin ? (
    <Navigate to="/login" replace state={{ from }} />
  ) : (
    children
  );
};

export default ProtectedRoute;
